package com.example.userserver.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.userserver.entity.CityInfo;
import com.example.userserver.entity.SchoolInfo;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.manager.CityInfoManager;
import com.example.userserver.manager.SchoolInfoManager;
import com.example.userserver.params.PageCityInfoParams;
import com.example.userserver.params.PageSchoolInfoParams;
import com.example.userserver.params.SchoolInfoParams;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.vo.CityInfoVO;
import com.example.userserver.vo.Result;
import com.example.userserver.vo.SchoolInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolInfoService {
    @Autowired
    private SchoolInfoManager schoolInfoManager;
    @Autowired
    private CityInfoManager cityInfoManager;
    public PageInfo<SchoolInfoVO> pageSchoolInfo(PageSchoolInfoParams pageSchoolInfoParams){
        PageHelper.startPage(pageSchoolInfoParams);
        List<SchoolInfoVO> list = schoolInfoManager.listSchoolInfo(pageSchoolInfoParams);
        List<CityInfoVO> cityInfoList = cityInfoManager.listCityInfo(new PageCityInfoParams());
        List<SchoolInfoVO> resultList = new ArrayList<>();
        for(int i = 0; i < list.size(); i++){
            for(int j=0; j < cityInfoList.size(); j++){
                SchoolInfoVO schoolInfoVO = new SchoolInfoVO();
                BeanCopyUtils.copyPropertiesIgnoreNull(list.get(i), schoolInfoVO);
                CityInfoVO cityInfoVO = new CityInfoVO();
                BeanCopyUtils.copyPropertiesIgnoreNull(cityInfoList.get(j), cityInfoVO);
                if(schoolInfoVO.getCityId().equals(cityInfoVO.getId())){
                    schoolInfoVO.setCityName(cityInfoVO.getCityName());
                    resultList.add(schoolInfoVO);
                }
            }
        }
        PageInfo<SchoolInfoVO> pageInfo = new PageInfo(resultList);
        return pageInfo;
    }

    public List<SchoolInfoVO> listSchoolInfo(String cityId){
        PageSchoolInfoParams pageSchoolInfoParams = new PageSchoolInfoParams();
        if( ! StringUtils.isEmpty(cityId)) {
            pageSchoolInfoParams.setCityId(cityId);
        }

        return schoolInfoManager.listSchoolInfo(pageSchoolInfoParams);
    }

    public SchoolInfoVO getSchoolInfo(String id) {
        return schoolInfoManager.getSchoolInfo(id);
    }

    public void deleteSchoolInfo(String id) {
        schoolInfoManager.deleteSchoolInfo(id);
    }

    public Result updateSchoolInfo(SchoolInfoParams schoolInfoParams) {
        SchoolInfo schoolInfo = new SchoolInfo();
        BeanCopyUtils.copyPropertiesIgnoreNull(schoolInfoParams, schoolInfo);
        schoolInfoManager.updateSchoolInfo(schoolInfo);
        return new Result(null, ReturnEnum.SUCCESS);
    }

    public Result insertSchoolInfo(SchoolInfoParams schoolInfoParams) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("school_name", schoolInfoParams.getSchoolName());
        SchoolInfo oldSchoolInfo = schoolInfoManager.selectSchool(queryWrapper);
        if(ObjectUtil.isNotEmpty(oldSchoolInfo)){
            return new Result(null , ReturnEnum.SCHOOLNAME_RE);
        }
        SchoolInfo schoolInfo = new SchoolInfo();
        BeanCopyUtils.copyPropertiesIgnoreNull(schoolInfoParams, schoolInfo);
        schoolInfoManager.insertSchoolInfo(schoolInfo);
        return new Result(null, ReturnEnum.SUCCESS);
    }

}
